import axios from 'axios'
import router from '../router'
import qs from 'qs'

// 创建axios实例
var instance = axios.create({ timeout: 5000 })

axios.defaults.baseURL = 'http://192.168.130.16:18517'

let token = ''

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(config => {
  console.log('进入了拦截器')
  const { url } = config
  if (url.includes('/basicLogin')) {
    console.log(config)
    return config
  }
  console.log('非登陆页面')
  token = sessionStorage.getItem('Authorization')
  console.log(token)
  token && (config.headers.authorization = token)
  console.log(config)
  return config
},
error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(
  response => {
    const { headers } = response

    if (headers && headers.authorization) {
      // 保存token
      sessionStorage.setItem('token', headers.authorization)
    }
    return response
  },
  error => {
    const status = error.response.status

    if (status) {
      if (status === 401) {
        alert('授权过期，请重新登录')
        sessionStorage.removeItem('Authorization')
        router.push({ path: '/login' })
        return
      }
      if (status === 503) {
        router.push({ name: '500' })
        return
      } else {
        alert('请求失败')
      }
      return
    }
    return Promise.reject(error)
  }
)

// GET
export const getRequest = (url, params) => {
  return axios({
    method: 'get',
    url: `${url}`,
    params,
    paramsSerializer: param => {
      return qs.stringify(param, { indices: false })
    }
  })
}

// POST
export const postRequest = (url, data) => {
  return axios({
    method: 'post',
    url,
    data,
    transformRequest: [
      // eslint-disable-next-line
      function(data) {
        return qs.stringify(data, { indices: false })
      }
    ],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export const postFormRequest = (url, formData) => {
  return axios({
    method: 'post',
    url,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const postBodyRequest = (url, data) => {
  return axios({
    method: 'post',
    url,
    data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

// PUT
export const putBodyRequest = (url, data) => {
  return axios({
    method: 'put',
    url,
    data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

export const putFormRequest = (url, formData) => {
  return axios({
    method: 'put',
    url,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const putRequest = (url, params) => {
  return axios({
    method: 'put',
    url,
    params,
    paramsSerializer: param => {
      return qs.stringify(param, { indices: false })
    }
  })
}

// DELETE
export const deleteBodyRequest = (url, data) => {
  return axios({
    method: 'delete',
    url,
    data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

export const deleteRequest = (url, params) => {
  return axios({
    method: 'delete',
    url,
    params,
    paramsSerializer: param => {
      return qs.stringify(param, { indices: false })
    }
  })
}

export const patchBodyRequest = (url, data) => {
  return axios({
    method: 'patch',
    url,
    data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

export const patchFormRequest = (url, formData) => {
  return axios({
    method: 'patch',
    url,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const patchRequest = (url, params) => {
  return axios({
    method: 'patch',
    url,
    params,
    paramsSerializer: param => {
      return qs.stringify(param, { indices: false })
    }
  })
}

// export file
export const getExportFileRequest = (url, params) => {
  return axios({
    method: 'get',
    url,
    params,
    responseType: 'blob',
    paramsSerializer: param => {
      return qs.stringify(param, { indices: false })
    }
  })
}

export const postExportFileRequest = (url, data) => {
  return axios({
    method: 'post',
    url,
    data,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

export const download = (url, params, onDownloadProgress) => {
  return axios({
    method: 'get',
    url,
    responseType: 'blob',
    onDownloadProgress
  })
}

export default instance
